package com.chenhy.controller;

import com.chenhy.pojo.User;
import com.chenhy.security.LoginUser;
import com.chenhy.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/userList")
    public String queryAllUser(Model model){
        List<User> users = userService.queryAllUser();
        model.addAttribute("userList",users);
        return "user/userList";
    }

    @GetMapping("ajaxList")
    public String ajaxList(int userRole, String roleName, Model model){
        List<User> users = userService.queryAllUser();
        List<User> ajaxUser = new ArrayList<>();
        if (userRole != 0){
            for (User user : users) {
                if (user.getUserRole() == userRole){
                    ajaxUser.add(user);
                }
            }
            model.addAttribute("userList",ajaxUser);
            return "user/userList.html::"+roleName;
        }
        model.addAttribute("userList",users);
        return "user/userList.html::"+roleName;
    }

    @GetMapping("/fuzzyQueryUser")
    public String userFuzzyQuery(String condition, Model model){
        List<User> users = userService.userFuzzyQuery(condition);
        model.addAttribute("userList",users);
        return "user/userList.html::allUser";

    }

    @Secured("ROLE_ADMIN")
    @PostMapping("/deleteUser")
    public String deleteUser(int id){
        userService.deleteUser(id);
        return "redirect:/user/userList";
    }

    @GetMapping("/userDetails")
    public String userDetails(int id, Model model){
        User user = userService.queryUserById(id);
        model.addAttribute("user",user);
        return "user/userDetails";
    }

    @GetMapping("/addUser")
    public String toAddUser(){
        return "user/addUser";
    }

    @Secured("ROLE_ADMIN")
    @PostMapping("/addUser")
    public String addUser(User user,Model model){
        userService.addUser(user);
        model.addAttribute("msg","添加成功，可继续添加");
        return "user/addUser";
    }

    @GetMapping("/updateUser")
    public String userUpdate(int id, Model model){
        User user = userService.queryUserById(id);
        model.addAttribute("user",user);
        return "user/updateUser";
    }

    @Secured("ROLE_ADMIN")
    @PostMapping("/updateUser")
    public String updateUser(User user){
        userService.updateUser(user);
        return "user/updateUser";
    }

    @Secured("ROLE_ADMIN")
    @PostMapping("/resetPassword")
    public String resetPasswordById(int id){
        userService.resetPasswordById(id);
        return "redirect:userList";
    }

    @GetMapping("/revisePassword")
    public String revisePassword(){
        return "user/revisePassword";
    }

    @PostMapping("/revisePassword")
    public String revisePassword(String oldPassword, String newPassword, Model model){
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        boolean isSuccess = userService.revisePassword(loginUser, oldPassword, newPassword);
        if (isSuccess){
            return "redirect:/logout";
        } else {
            model.addAttribute("msg", "修改失败，旧密码不正确");
            return "user/revisePassword";
        }
    }

    @Secured("ROLE_ADMIN")
    @GetMapping("/flushCache")
    public String flushCache(){
        userService.flushCache();
        return "redirect:/index";
    }



}
